<template>
  <div>
    <div class="my">
      <div class="bg">
        <img alt="" src="@/assets/avatar.png"/>
      </div>
      <div class="my-info">
        <div class="my-pic">
          <img alt="" src="http://liufusong.top:8080/img/profile/avatar.png"/>
        </div>
        <div class="username">
          <p>{{ username }}</p>
        </div>
        <div class="login">
          <button @click="delLogin">{{ flag ? '退出' : '登录' }}</button>
        </div>
        <div v-show="!flag" class="set-info"><span>编辑个人资料 ▶</span></div>
      </div>
    </div>
    <div class="my-list">
      <van-grid :border="false" :column-num="3">
        <van-grid-item icon="star-o" router text="我的收藏" to='/shoucang'/>
        <van-grid-item icon="wap-home-o" router text="我的出租" to="/chuzu"/>
        <van-grid-item icon="clock-o" text="看房记录"/>
        <van-grid-item icon="debit-pay" text="成为房主"/>
        <van-grid-item icon="user-circle-o" text="个人资料"/>
        <van-grid-item icon="service-o" text="联系我们"/>
      </van-grid>
    </div>
    <div class="ad">
      <img alt="" src="@/assets/join.png"/>
    </div>
  </div>
</template>

<script>
import { getuserInfo } from '@/api/user'
import { delToken } from '@/utils/storage'

export default {
  name: 'MyIndex',

  data () {
    return {
      username: '',
      flag: true
    }
  },
  async created () {
    const res = await getuserInfo()
    // console.log(res)
    this.username = res.body.nickname
  },

  methods: {
    async delLogin () {
      try {
        await this.$dialog.confirm({
          title: '确定退出登录'
        })
        // 1清除token
        delToken()
        // 2跳转登陆页面
        this.$router.push('/login')
      } catch (err) {
        console.log('取消', err)
      }
    }
  }
}
</script>

<style lang="less" scoped>
.my {
  position: relative;
  width: 100%;
  height: 300px;

  .bg {
    height: 300px;
    overflow: hidden;

    img {
      width: 100%;
    }
  }

  .my-info {
    position: absolute;
    top: 45%;
    left: 50%;
    transform: translateX(-50%);
    width: 320px;
    height: 165px;
    background-color: #fff;
    box-shadow: 0 0 10px 3px #ddd;

    .my-pic {
      position: absolute;
      top: -20%;
      left: 50%;
      transform: translateX(-50%);
      width: 60px;
      height: 60px;
      // background-color: #f5f5f5;
      border: 6px solid #f5f5f5;
      border-radius: 50%;
      box-shadow: 0 2px 2px #bdbdbd;
      overflow: hidden;

      img {
        width: 100%;
      }
    }

    .username {
      margin-top: 60px;
      width: 100%;

      p {
        text-align: center;
        font-size: 14px;
      }
    }

    .login {
      margin-top: 10px;
      width: 100%;

      button {
        display: block;
        margin: 0 auto;
        width: 70px;
        height: 30px;
        border-radius: 3px;
        background-color: #21b97a;
        border: none;
        box-shadow: none;
        color: #fff;
        font-size: 14px;
      }
    }

    .set-info {
      margin-top: 10px;
      width: 100%;
      text-align: center;
      font-size: 14px;
      color: #999;
    }
  }
}

.my-list {
  margin-top: 10px;
  width: 100%;
  height: 190px;
}

.ad {
  width: 345px;
  height: 85px;
  margin: 0 auto;
  border-radius: 3px;
  overflow: hidden;

  img {
    width: 100%;
  }
}
</style>
